package com.winbook.layer.controller;

import java.sql.Timestamp;
import java.util.Calendar;
import java.util.Date;

import org.json.JSONException;
import org.json.JSONObject;
import org.restlet.data.Form;
import org.restlet.data.Status;
import org.restlet.ext.json.JsonRepresentation;
import org.restlet.representation.Representation;
import org.restlet.resource.Delete;
import org.restlet.resource.Get;
import org.restlet.resource.Post;
import org.restlet.resource.ServerResource;

import com.winbook.domainobjects.User;
import com.winbook.domainobjects.WinCondition;
import com.winbook.layer.dataaccess.WinConditionDAO;



public class WinConditionResource extends ServerResource {
	
	WinConditionDAO wcDAO;
	
	@Get("html")
	public String getWinConditionHTML()
	{
		return "<h1>TO BE IMPLEMENTED</h1>";
	}
	
	@SuppressWarnings("deprecation")
	@Post
	public Representation createNewWinCondition(Representation entity)
	{
		Form winConditionForm = new Form(entity);
		String winConditionText = winConditionForm.getFirstValue("wincondition");
		String wallName = winConditionForm.getFirstValue("wall");
		String projectName = getRequestAttributes().get("project").toString();
				
		String authorEmail = getRequest().getChallengeResponse().getIdentifier();//getClientInfo().getUser();
		
		wcDAO = new WinConditionDAO(projectName, wallName);
		
		WinCondition winCondition = new WinCondition(new User(authorEmail), winConditionText);
		
		int wcid = wcDAO.addWinCondition(winCondition);
		
		JSONObject json = new JSONObject();
		try {
			json.put("wcid", wcid);
			json.put("timestamp", new Timestamp(100000));
			setStatus(Status.SUCCESS_CREATED);
		} catch (JSONException e) {
			e.printStackTrace();
			setStatus(Status.SERVER_ERROR_INTERNAL, "There has been an error creating the Win Condition. Please send feedback about the same");
			
		}
		
		
		return new JsonRepresentation(json);
	}
	
	@Delete("json|application/json")
	public void deleteWinCondition(JsonRepresentation entity)
	{
		JSONObject deletionConfirmation;
		
		try {
			
			
			JSONObject winConditionToDelete = entity.getJsonObject();
			int wcidToDelete = winConditionToDelete.getInt("wcid");
			String action = winConditionToDelete.getString("action"); //expecting action to be "delete" only for security reasons
			String wallName = winConditionToDelete.getString("wall");
			String username = getRequest().getChallengeResponse().getIdentifier();
			String projectName = getRequestAttributes().get("project").toString();
			
			System.out.println("DELETE accepted! Deletion by :"+username+" Of Win Condition id: "+wcidToDelete+" action = "+action+" wall = "+wallName);
			
			if(action.compareToIgnoreCase("delete")!=0)
			{
				setStatus(Status.CLIENT_ERROR_NOT_ACCEPTABLE, "Action not specified for deletion");
				return;
			}
			
			wcDAO = new WinConditionDAO(projectName, wallName);
			
			if(wcDAO.deleteWinCondition(wcidToDelete))
				setStatus(Status.SUCCESS_OK);
			
			else 
				setStatus(Status.SERVER_ERROR_INTERNAL, "Unable to delete win condition");
			
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			setStatus(Status.CLIENT_ERROR_BAD_REQUEST, "Incorrect delete request. Malformed JSON");
			return;
		}
		
		
		
		
		
	}
	
	
	
	
}
